/**
 * Created by jiangqifan on 16/3/2.
 */
/**
 * Created by jiangqifan on 16/3/2.
 */

var mongoose = require('mongoose');
module.exports.open = function(config) {

    // // Connect to the tasklocker MongoDB
    // mongoose.connect(config.url, config.config, function(err) {
    //     if(err) {
    //         console.log('connection error', err);
    //     } else {
    //         console.log('connection successful');
    //     }
    // });

    // mongoose.connect();
    var db = mongoose.connection;
    var host = config.host;
    var port = config.port;
    var database = config.database;
    var options = config.options;
    db.open(host, database, port, options);
    db.on('error', function (err) {
        //console.log("connect error :" + err);
        //监听BAE mongodb异常后关闭闲置连接
        db.close();
    });
    //监听db close event并重新连接
    db.on('close', function () {
        console.log("connect close retry connect ");
        db.open(host, database, port, options);
    });
    return db;
}